<?php
/*
	提现
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
global $_RQ;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
$return['ischecklogin']=cfc('core')->checklogin('','',true);
$credit_types=credit_types(true);
if($_RQ['op']=='get'){
	$up_type=$tixianset['up_type']?:3;
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid']));
	$tinum=intval($return['allmoney']/$tixianset['man_fee']);
	$return['man_fee']=$tixianset['man_fee'];
	$return['member']=$member;
	$return['params']['type']=$_RQ['type']?:3;
	$return['allmoney']=$member['credit'.$return['params']['type']];
	$return['typetitle']=$credit_types[$return['params']['type']];
	if($return['params']['type']==3){
		$tips='购买商品，参与分享都可以获得《'.$return['typetitle'].'》哦！';
	}elseif($return['params']['type']==4){
		$tips='可以充值获得《'.$return['typetitle'].'》哦！';
	}if($return['params']['type']==6){
		$tips='签到，直推人参与拼团，购买指定商品，都可以获得《'.$return['typetitle'].'》哦！';
	}
	$return['tips']=$tips;
	$return['is_open']=$tixianset['is_open'];
	$return['open_upbaofee']=$tixianset['open_upbaofee'];
	$return['post']=array('type'=>$return['params']['type']);
	$pay=uni_setting('pay');
	if($return['post']['type']!=1){
		$jifen_rate=$pay['credit'.$return['post']['type'].'_apirate']?:1;
		$return['cancongzhi']=$pay['credit'.$return['post']['type'].'_api'];
	}else{
		$cash=uni_setting('cash');
		$jifen_rate=$cash['jifen_rate']?:1;
		if($cash['jifencz_type']){
			if($cash['jifencz_type']==1){
				$return['cancongzhi']=1;
			}elseif(check_account_permission('jy_merch')){
				$merch=pdo_get('jy_merch_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');
				if($merch['id']){
					$return['cancongzhi']=1;
				}
			}
		}
		$return['openjifencash']=$cash['openjifen'];
	}
	$return['jifen_rate']=intval($jifen_rate*100)/100;
	$pay=uni_setting('pay');
	$return['iszhuan']=$pay['credit'.$return['params']['type'].'_zhuan'];
	$setting=uni_setting('register');
	$iscode=$setting['dxmb'] && $setting['login_type']?1:0;
	$return['post']['iscode']=$iscode;
	if($_RQ['isrecharge']){
		$return['showrecharge']=true;
	}
	set_html('title','我的'.$return['typetitle']);
	exi($return);
}
if($_RQ['op']=='fenlist'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']);
	if($_RQ['type']){
		$params['type']=intval($_RQ['type']);
	}
    $orderby='createtime desc';
	$total='';
	$list=pdo_getslice('core_credit_log',$params,array($pindex,$psize),$total,array('id','type','is_add','fee','remark','createtime'),'','id desc');
	if($list){
		foreach($list as &$item){
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['is_addtitle']=$item['is_add']?'获得':'消费';
			$item['typetitle']=$credit_types[$item['type']];
		}
	}
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='zhuan'){
	$post=get_postdata();
	$type=$post['type'];
	if(!$type){
		exi('请选择要转账的种类！','erroe');
	}
	$pay=uni_setting('pay');
	$iszhuan=$pay['credit'.$type.'_zhuan'];
	if(!$iszhuan){
		exi('该虚拟币不支持转账！','erroe');
	}
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid']));
	if(!$post['fee']){
		exi('请输入转账'.$this->db_title.'！','erroe');
	}
	if(!$post['mobile']){
		exi('请输入收款人账户/手机号！','erroe');
	}
	if((!$post['password'] || $post['password']!=$member['paypassword']) && !$post['nopassword']){
		exi('支付密码不正确！','erroe');
	}
	if($member['credit'.$type]<$post['fee']){
		exi('转账额度不能大于，当前积分额度！','erroe');
	}
	$tomember=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'username'=>$post['mobile']));
	$tomid=$tomember['id'];
	if(!$tomid){
		$tomember=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'mobile'=>$post['mobile']));
		$tomid=$tomember['onemid']?:$tomember['id'];
	}
	if(!$tomid){
		exi('没找到收款人，转账失败！','erroe');
	}
	model('core/member')->member_credit($_SESSION['uid'],'credit'.$type,$post['fee'],false,'core','转账给:'.$post['mobile']);
	model('core/member')->member_credit($tomid,'credit'.$type,$post['fee'],true,'core',$member['username'].'的转账');
	exi('转账成功！');
}
if($_RQ['op']=='recharge'){
	$post=get_postdata();
	if(intval($post['jifen'])<=0){
		exi('请先设置额度！并且为整数！','error');
	}
	if($post['type']!=1){
		$setting=uni_setting('pay');
		$jifen_rate=$setting['credit'.$post['type'].'_apirate']?:1;
	}else{
		$cash=uni_setting('cash');
		$cash['jifen_rate']=$cash['jifen_rate']?:1;
	}
	$jifen_rate=intval($jifen_rate*100)/100;
	$fee=$post['jifen']/$jifen_rate;
	$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'mid'=>$_SESSION['uid'],
		'couponid'=>intval($post['couponid']),
		'fee'=>$fee,
		'credit'=>$post['jifen'],
		'type'=>$post['type'],
		'wxapp_id'=>$_SESSION['appgid'],
		'createtime'=>TIMESTAMP
	);
	pdo_insert('core_recharge',$data);
	$id=pdo_insertid();
	$params=array(
		'action'=>'recharge',
		'id'=>$id,
		'fee'=>$fee,
		'title'=>'币充值',
		'plugin'=>'core',
		//'pay_type'=>'wechat',
		'no_type'=>array('hdfk'),
		'back_url'=>'core/member.xunibi/pay_result',
		'success_url'=>'core/member.xunibi//type='.$post['type'],
		'error_url'=>'core/member.xunibi//type='.$post['type'],
	);
	if($_SESSION['container']=='aliapp' && $_SESSION['aliappopenid']){
		$params['pay_type']='alipay';
	}
	$pay_id=cfc('pay')->create_pay_order($params);
	pdo_update('core_recharge',array('pay_id'=>$pay_id),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi($pay_id);
}
if($_RQ['op']=='password'){
	$post=get_postdata();
	if(!$post['password']){
		exi('请输入支付密码！','erroe');
	}
	if($post['iscode'] && !check_sms_code($post['smscode'])){
		exi('手机验证码错误！不能修改密码！','error');
	}
	pdo_update('core_members',array('paypassword'=>$post['password']),array('id'=>$_SESSION['uid']));
	exi('设置成功！');
}
if($_RQ['op']=='pay_result'){
	$payresult=$_SESSION['pay_result'];
	$recharge=pdo_get('core_recharge',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	if($recharge['fee']!=$payresult['fee']){
		exi('支付失败，支付金额不匹配！','error');
	}
	if($recharge['status']){
		exi('该订单已支付！无需重复支付！','error');
	}
	if($payresult['status']!=1){
		exi('充值失败！','error');
	}
	pdo_update('core_recharge',array('status'=>1,'pay_type'=>$payresult['pay_type']),array('id'=>$recharge['id'],'uniacid'=>$_SESSION['uniacid']));
	cfc('core')->member_credit($_SESSION['uid'],'credit'.$recharge['type'],$recharge['credit'],1,'member','会员自助充值');
	if($payresult['pay_type']=='ttpay'){
		//抖音小程序同步订单
		$wxapp=pdo_get('core_account_wxapp',array('id'=>$recharge['wxapp_id'],'uniacid'=>$_SESSION['uniacid']),array('id','tt_appid','tt_paymchid','tt_paysecret'));
		$setting=array(
				'id'=>$wxapp['id'],
				'app_id'=>$wxapp['tt_appid'],
				'tt_paymchid'=>$wxapp['tt_paymchid'],
				'tt_paysecret'=>$wxapp['tt_paysecret'],
		);
		if($wxapp){
			$recharge['list'][]=array(
				'goodsid'=>108,
				'image'=>tomedia('../core/resource/images/welcomehdp02.jpg'),
				'title'=>'会员充值',
				'price'=>$recharge['fee'],
			);
			$recharge['status']=1;
			$recharge['price']=$recharge['fee'];
			$recharge['detail_url']='core/member/credit_log/index';
			$recharge['tid']=pdo_getcolumn('core_paylog',array('id'=>$recharge['pay_id'],'uniacid'=>$_SESSION['uniacid']),'tid');
			cfc('tt_pay',$setting)->push_order($recharge);
		}
	}
	exi('充值成功！');
}